module.exports = Status;

Status.statusID = {
	Idle:0,		// 空闲
	Bid:1,		// 叫分
	Grab:2,		// 抢地主
	Playing:3,	// 开局
	Over:4,		// 结算
	Loss:5,		// 流局
}
var statusName = ['Idle','Bid','Grab','Playing','Over','Loss'];
function Status(id){
	this._id = id;
}

// 返回指定状态的超时时间
Status.getTimeOut = function(id){
	return statusTimeout[id];
}

Status.createState = function(id){
	console.log('createState:',id)
	var ss = [Idle,Bid,Grab,Playing,Over,Loss];
	var obj = new ss[id];
	obj.id = id;
	return obj;
}
Status.getStateName = function(id){
	return statusName[id];
}
///空闲状态
function Idle( ) {
}
Idle.prototype.onTimeOut = function( table ) {
	table.setState(Status.statusID.Bid);
};
Idle.prototype.onEnter = function( table ) {
}
Idle.prototype.onExit = function( table ) {
}

///叫分状态
function Bid( ) {	
}
Bid.prototype.onEnter = function( table ) {
	console.log('进入叫分状态',table);
	table.onStart();
	// setTimeout(function(){
	// 	table.setState(Status.statusID.Playing);
	// }, 3000);
}
Bid.prototype.onExit = function( table ) {
	console.log('离开叫分状态');
}

///抢地主
function Grab( ) {
}
Grab.prototype.onEnter = function( table ) {

	// 1,先找出可以参与抢地主的玩家,排除PASS的玩家
	console.log(table._askInfo);
	var info = table._askInfo;
	var temp = [0,1,2];	// candidate list
	for (var i in info) {
		if(info[i].chip==0)
			temp.splice(info[i].id,1);
	}
	table._dzid = table._playID;
	console.log('地主人选:',temp)
	if (temp.length==1){
		console.log('只有一个玩家叫分,所以直接进入游戏状态')
		return table.setState(Status.statusID.Playing);
	}
	table._askInfo = [];
	table._dzList = temp;
	console.log('当前由'+table._playID+'叫地主');
	
	// temp 中保存可以参与抢地主的座位ID
	for (var i = 0; i < temp.length; i++) {
		if(table._playID == temp[i])
		{
			table._playID = i+1;
			break;
		}
	}
	console.log('由'+table._playID+'开始抢地主',temp);
}
Grab.prototype.onExit = function( table ) {
}

///开局状态
function Playing( ) {
}

Playing.prototype.onEnter = function( table ) {
	setTimeout(function(){table.setState(Status.statusID.Over)}, 3000);
}
Playing.prototype.onExit = function( table ) {
	console.log('离开游戏状态');
}

///结算状态
function Over( ) {
}
Over.prototype.onEnter = function( table ) {
	setTimeout(function(){table.restart()}, 3000);
}
Over.prototype.onExit = function( table ) {
	console.log('离开结算状态');
}

///流局状态
function Loss( ) {
}
Loss.prototype.onEnter = function( table ) {
	table.restart()
}
Loss.prototype.onExit = function( table ) {
}
